package com.jiegui.modules.wechat.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.jiegui.common.core.domain.AjaxResult;
import com.jiegui.common.utils.DateUtils;
import com.jiegui.modules.wechat.WxChartService;
import com.jiegui.modules.wechat.model.LoginModel;
import com.jiegui.modules.common.annotation.Login;
import com.jiegui.modules.common.annotation.LoginUser;
import com.jiegui.system.domain.SystemConfig;
import com.jiegui.system.service.ISystemConfigService;
import com.jiegui.wx.domain.WechatGrouping;
import com.jiegui.wx.domain.WechatOnlineService;
import com.jiegui.wx.domain.WechatUser;
import com.jiegui.wx.service.WechatGroupingService;
import com.jiegui.wx.service.WechatOnlineServiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 小程序登入，问题，充值，资金接口
 * @author: wuqicheng
 * @time: 09/12/2021 9:19 AM
 */
@RestController
@Slf4j
@RequestMapping("/api/mini")
public class WxChartController {
    @Resource
    WxChartService wxChartService;

    @Resource
    WechatGroupingService wechatGroupingService;

    @Resource
    WechatOnlineServiceService wechatOnlineService;


    @Resource
    ISystemConfigService systemConfigService;


    /**
     * @param request:
     * @param model:
     * @return AjaxResult
     * @Author 小程序登入
     * @Description Description
     * @Date 2:23 PM 14/12/2021
     **/
    @RequestMapping("/login")
    @Login
    public AjaxResult login(HttpServletRequest request, LoginModel model) {

        try {
            return AjaxResult.success("data", wxChartService.login(model, request));
        } catch (Exception e) {
            log.error("微信登入异常：" + e);
            return AjaxResult.error();
        }

    }

    /**
     * @param
     * @return com.jiegui.common.core.domain.AjaxResult
     * @Author wuqicheng
     * @Description 微信客服获取问题分组
     * @Date 2:27 PM 14/12/2021
     **/
    @RequestMapping("/queryQuestionGroup")
    @Login
    public AjaxResult queryQuestionGroup() {

        return AjaxResult.success("data", wechatGroupingService.list(new LambdaQueryWrapper<WechatGrouping>().eq(WechatGrouping::getDelFlag, "0").orderByAsc(WechatGrouping::getSort)));
    }

    /**
     * @param groupId:
     * @return com.jiegui.common.core.domain.AjaxResult
     * @Author wuqicheng
     * @Description Description
     * @Date 2:29 PM 14/12/2021
     **/
    @RequestMapping("/queryQuestionList")
    @Login
    public AjaxResult queryQuestionList(@RequestParam("groupId") Long groupId) {
        WechatOnlineService onlineService = new WechatOnlineService();
        onlineService.setGroupingId(groupId);
        onlineService.setDelFlag("0");
        return AjaxResult.success("data", wechatOnlineService.selectWechatOnlineServiceList(onlineService));
    }

    /**
     * @param id:
     * @return com.jiegui.common.core.domain.AjaxResult
     * @Author wuqicheng
     * @Description 根据客服问题获取答案
     * @Date 2:44 PM 14/12/2021
     **/
    @RequestMapping("/queryAnswer")
    @Login
    public AjaxResult queryAnswer(@RequestParam("id") Long id) {
        WechatOnlineService onlineService = wechatOnlineService.getById(id);
        Map<String, Object> map = new HashMap<>();
        if (onlineService != null) {
            map.put("answer", onlineService.getAnswer());
            map.put("type", onlineService.getIdentification());
        } else {
            map.put("answer", "未找到解决方案，请联系在线客服");
            map.put("type", "txt");
        }
        return AjaxResult.success("data", map);
    }

    /**
     * @param type:
     * @return com.jiegui.common.core.domain.AjaxResult
     * @Author wuqicheng
     * @Description 获取协议
     * @Date 2:03 PM 29/12/2021
     **/
    @RequestMapping("/queryTreaty")
    @Login
    public AjaxResult queryTreaty(@RequestParam("type") String type) {
        SystemConfig config = systemConfigService.getOne(new LambdaQueryWrapper<SystemConfig>().eq(SystemConfig::getParamKey, type));
        return AjaxResult.success("data",config.getParamValue());
    }

}
